import Vue from 'vue'
import router from './router'
import store from './store'
import axios from 'axios'
import Element from 'element-ui'

axios.defaults.baseURL = 'http://localhost:8081'
Vue.prototype.$axios = axios

axios.interceptors.request.use(config => {
    return config
})

axios.interceptors.response.use(response => {
    var res = response.data
    if(res.code === 200){
        return response
    }else{
        Element.Message.error(res.msg, {duration: 3*1000})
        return new Promise.reject(res.msg)
    }
}, error => {
    if(error.response.data){
        error.message = error.response.data.msg
    }

    if(error.response.status === 401){
        store.commit("removeInfo")
        router.push('/login')
    }
    Element.Message.error(error.message, {duration: 3 * 1000})
    return new Promise.reject(error)
})